package jejunu.androboys.QuickCall.Component;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class AddressBook 
{
	final int NAME=0;
	final int PHONE_NUM=1;
	final int NUM_TYPE=2;
	
	Activity mAct;
	String[][] mBook;
	String[] mType_arr;

	public AddressBook(Activity act) 
	{
		mAct=act;
		
		mType_arr=new String[]{"집", "휴대전화", "직장", "직장팩스", "집팩스", 
				"호출기", "기타", "맞춤설정", "콜백", "카폰", "회사기본전화", "ISDN",
				"기본", "기타팩스", "무선통신", "텔렉스", "TTY TDD", "직장휴대전화",
				"직장호출기", "비서", "MMS"};
        
		createBook();
	}
	
	public void createBook()
	{
		Uri phone_data=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		String[] projection=new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
//		String selection=ContactsContract.CommonDataKinds.Phone.TYPE+"="+ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
		String selection=null;
		String sortOrder=ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC";
		
		Cursor p_cursor = mAct.managedQuery(phone_data, projection, selection, null, sortOrder);
		
		int addressCount=p_cursor.getCount();
        
        mBook=new String[addressCount][3];
        
        int count=0;
        
		if(p_cursor.moveToFirst())
		{
			do 
			{
				mBook[count][NAME]=p_cursor.getString(NAME);
	    		mBook[count][PHONE_NUM]=p_cursor.getString(PHONE_NUM);
//	    		mBook[count][NUM_TYPE]=p_cursor.getString(NUM_TYPE);
	    		
	    		int type_num=Integer.parseInt(p_cursor.getString(NUM_TYPE));
	    		mBook[count][NUM_TYPE]=mType_arr[type_num-1];
	    		
	    		count++;
				
			} while (p_cursor.moveToNext() || count>addressCount);
		}
	}
	
	public String[][] getBook()
	{
		return mBook;
	}
}
